package com.quanyan.travel.biz.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.travel.biz.BizTravelOrderService;
import com.quanyan.travel.constants.OutdoorConstants;
import com.quanyan.travel.entity.*;
import com.quanyan.travel.entity.vo.Master;
import com.quanyan.travel.entity.vo.ProductDetail;
import com.quanyan.travel.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("bizTravelOrderService")
public class BizTravelOrderServiceImpl implements BizTravelOrderService {

    @Autowired
    private VTravelUserDefinedMapper vTravelUserDefinedMapper;

    @Autowired
    private TbOutdoorOrderMapper tbOutdoorOrderMapper;

    @Autowired
    private TbOutdoorDiscountMapper tbOutdoorDiscountMapper;

    @Autowired
    private TbOutdoorSignUserMapper tbOutdoorSignUserMapper;

    @Autowired
    private TbOutdoorActivityMapper tbOutdoorActivityMapper;

    @Autowired
    private TbOutdoorActivityDescMapper tbOutdoorActivityDescMapper;

    @Autowired
    private TbOutdoorActivityMasterMapper tbOutdoorActivityMasterMapper;

    @Autowired
    private TbOutdoorProductMapper tbOutdoorProductMapper;

    @Autowired
    private TbOutdoorThemeMapper tbOutdoorThemeMapper;

    @Autowired
    private TbOutdoorDestinationMapper tbOutdoorDestinationMapper;

    @Autowired
    private TbOutdoorActivityDestinationMapper tbOutdoorActivityDestinationMapper;



    @Override
    public List<TbOutdoorActivity> queryActivityByIds(List<Integer> ids) {
        TbOutdoorActivityExample example = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        return tbOutdoorActivityMapper.selectByExample(example);
    }

    @Override
    public TbOutdoorActivity queryActivityById(Integer id) {
        return tbOutdoorActivityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbOutdoorProduct> queryProductByIds(List<Integer> ids) {
        TbOutdoorProductExample example = new TbOutdoorProductExample();
        TbOutdoorProductExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        return tbOutdoorProductMapper.selectByExample(example);
    }

    @Override
    public List<ProductDetail> queryProductDetailByIds(List<Integer> ids) {
        return vTravelUserDefinedMapper.queryProductDetail(ids);
    }

    @Override
    public TbOutdoorDiscount queryTbOutdoorDiscountById(Integer id) {
        return tbOutdoorDiscountMapper.selectByPrimaryKey(id);
    }

    @Override
    public TbOutdoorDiscount queryTbOutdoorDiscountByActivityId(Integer id) {
        TbOutdoorDiscountExample example = new TbOutdoorDiscountExample();
        TbOutdoorDiscountExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        criteria.andActivityIdEqualTo(id);
        List<TbOutdoorDiscount> discounts = tbOutdoorDiscountMapper.selectByExample(example);
        if (null != discounts && discounts.size() == 1) {
            return discounts.get(0);
        }
        return null;
    }

    @Override
    public boolean updateTravelStorage(Integer id, int num) {
        return vTravelUserDefinedMapper.updateTravelStorage(id, num) > 0;
    }

    @Override
    public boolean recoveryTravelStorage(Integer id, int num) {
        return vTravelUserDefinedMapper.recoveryTravelStorage(id, num) > 0;
    }

    @Override
    public int updateTbOutdoorActivity(List<Integer> ids) {
        TbOutdoorActivityExample example = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        TbOutdoorActivity activity = new TbOutdoorActivity();
        activity.setIsDelete((byte) 1);
        return tbOutdoorActivityMapper.updateByExampleSelective(activity, example);
    }

    @Override
    public int updateNotShwoActivity(List<Integer> ids) {
        TbOutdoorActivityExample example = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        criteria.andIsIndexShowEqualTo(OutdoorConstants.INDEX_SHOW_TRUE);
        TbOutdoorActivity activity = new TbOutdoorActivity();
        activity.setIsIndexShow(OutdoorConstants.INDEX_SHOW_FALSE);
        return tbOutdoorActivityMapper.updateByExampleSelective(activity, example);
    }

    @Override
    public int updateTbOutdoorProduct(List<Integer> activityIds) {
        TbOutdoorProductExample example = new TbOutdoorProductExample();
        TbOutdoorProductExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdIn(activityIds);
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        TbOutdoorProduct product = new TbOutdoorProduct();
        product.setIsDelete(OutdoorConstants.IS_DELETE);
        return tbOutdoorProductMapper.updateByExampleSelective(product, example);
    }

    @Override
    public int updateThemeProduct(List<Integer> productIds) {
        TbOutdoorThemeExample example = new TbOutdoorThemeExample();
        TbOutdoorThemeExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdIn(productIds);
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        TbOutdoorTheme product = new TbOutdoorTheme();
        product.setIsDelete(OutdoorConstants.IS_DELETE);
        return tbOutdoorThemeMapper.updateByExample(product, example);
    }

    @Override
    public TbOutdoorOrder insertTravelOrder(TbOutdoorOrder tbOutdoorOrder) {
        tbOutdoorOrderMapper.insertSelective(tbOutdoorOrder);
        return tbOutdoorOrder;
    }


    @Override
    public TbOutdoorOrder queryOutDoorOrderByOrderNo(String orderNo) {
        TbOutdoorOrderExample example = new TbOutdoorOrderExample();
        TbOutdoorOrderExample.Criteria criteria = example.createCriteria();
        criteria.andOutdoorOrderNoEqualTo(orderNo);
        List<TbOutdoorOrder> list = tbOutdoorOrderMapper.selectByExample(example);
        if (list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public int queryOutDoorOrderNumByUid(Integer uid) {
        TbOutdoorOrderExample example = new TbOutdoorOrderExample();
        TbOutdoorOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        return tbOutdoorOrderMapper.countByExample(example);
    }

    @Override
    public List<TbOutdoorOrder> queryOutDoorOrderListByUid(Integer uid, Integer pageNum, Integer pageSize) {
        TbOutdoorOrderExample example = new TbOutdoorOrderExample();
        TbOutdoorOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        example.setOrderByClause("create_time desc");
        example.setPage(new Page(pageNum*pageSize, pageSize));
        return tbOutdoorOrderMapper.selectByExample(example);
    }

    @Override
    public TbOutdoorOrder updateTbOutDoorOrder(TbOutdoorOrder tbOutdoorOrder) {
        tbOutdoorOrderMapper.updateByPrimaryKeySelective(tbOutdoorOrder);
        return tbOutdoorOrder;
    }

    @Override
    public TbOutdoorOrder updateTbOutDoorOrderByTradeOrderNo(TbOutdoorOrder tbOutdoorOrder, String tradeOrderNo) {
        TbOutdoorOrderExample example = new TbOutdoorOrderExample();
        TbOutdoorOrderExample.Criteria criteria = example.createCriteria();
        criteria.andTradeOrderNoEqualTo(tradeOrderNo);
        int i= tbOutdoorOrderMapper.updateByExampleSelective(tbOutdoorOrder, example);
        if (1 == i){
            List<TbOutdoorOrder> orders = tbOutdoorOrderMapper.selectByExample(example);
            if (null != orders && orders.size() == 1) {
                return orders.get(0);
            }
        }
        return null;
    }

    @Override
    public int updateTbOutDoorOrderByOrderNo(TbOutdoorOrder tbOutdoorOrder, String orderNo) {
        TbOutdoorOrderExample example = new TbOutdoorOrderExample();
        TbOutdoorOrderExample.Criteria criteria = example.createCriteria();
        criteria.andOutdoorOrderNoEqualTo(orderNo);
        return tbOutdoorOrderMapper.updateByExampleSelective(tbOutdoorOrder, example);
    }

    @Override
    public boolean cancelSignUserByOrderId(Integer orderId) {
        TbOutdoorSignUserExample example = new TbOutdoorSignUserExample();
        TbOutdoorSignUserExample.Criteria criteria =example.createCriteria();
        criteria.andOutdoorOrderIdEqualTo(orderId);
        TbOutdoorSignUser tbOutdoorSignUser = new TbOutdoorSignUser();
        tbOutdoorSignUser.setIsCancel(OutdoorConstants.DEFAULT_STATUS_BYTE_YES);
        return tbOutdoorSignUserMapper.updateByExampleSelective(tbOutdoorSignUser, example) > 0;
    }

    @Override
    public List<TbOutdoorOrder> queryExpireOrder() {
        TbOutdoorOrderExample example = new TbOutdoorOrderExample();
        TbOutdoorOrderExample.Criteria criteria = example.createCriteria();
        criteria.andDeadTimeLessThanOrEqualTo(new Date());
        criteria.andOrderDisplayStatusEqualTo(OutdoorConstants.APP_TRAVEL_ORDER_WAIT_PAY);
        return tbOutdoorOrderMapper.selectByExample(example);
    }

    @Override
    public List<TbOutdoorOrder> queryPayingOrder() {
        TbOutdoorOrderExample example = new TbOutdoorOrderExample();
        TbOutdoorOrderExample.Criteria criteria = example.createCriteria();
        criteria.andDeadTimeBetween(new Date(), DateUtils.addTime(new Date(), 5, DateUtils.TIME_TYPE_MINUTE));
        criteria.andPayWaitEqualTo(OutdoorConstants.TRAVEL_ORDER_PAYING);
        criteria.andIsCloseEqualTo(OutdoorConstants.DEFAULT_STATUS_byte_NO);
        return tbOutdoorOrderMapper.selectByExample(example);
    }

    @Override
    public List<Master> queryOutdoorMaster(List<Integer> ids) {
        return vTravelUserDefinedMapper.queryOutdoorMaster(ids);
    }

    @Override
    public TbOutdoorActivity insertTbOutdoorActivity(TbOutdoorActivity tbOutdoorActivity) {
        tbOutdoorActivityMapper.insertSelective(tbOutdoorActivity);
        return tbOutdoorActivity;
    }

    @Override
    public TbOutdoorActivityDesc insertTbOutdoorActivityDesc(TbOutdoorActivityDesc tbOutdoorActivityDesc) {
        tbOutdoorActivityDescMapper.insertSelective(tbOutdoorActivityDesc);
        return tbOutdoorActivityDesc;
    }

    @Override
    public TbOutdoorActivityMaster insertTbOutdoorActivityMaster(TbOutdoorActivityMaster tbOutdoorActivityMaster) {
        tbOutdoorActivityMasterMapper.insertSelective(tbOutdoorActivityMaster);
        return tbOutdoorActivityMaster;
    }

    @Override
    public TbOutdoorProduct insertTbOutdoorProduct(TbOutdoorProduct tbOutdoorProduct) {
        tbOutdoorProductMapper.insertSelective(tbOutdoorProduct);
        return tbOutdoorProduct;
    }

    @Override
    public TbOutdoorDiscount insertTbOutdoorDiscount(TbOutdoorDiscount tbOutdoorDiscount) {
        tbOutdoorDiscountMapper.insertSelective(tbOutdoorDiscount);
        return tbOutdoorDiscount;
    }

    @Override
    public TbOutdoorTheme insertTbOutdoorTheme(TbOutdoorTheme tbOutdoorTheme) {
        tbOutdoorThemeMapper.insertSelective(tbOutdoorTheme);
        return tbOutdoorTheme;
    }

    @Override
    public TbOutdoorSignUser insertTbOutdoorSignUser(TbOutdoorSignUser tbOutdoorSignUser) {
        tbOutdoorSignUserMapper.insertSelective(tbOutdoorSignUser);
        return tbOutdoorSignUser;
    }

    @Override
    public TbOutdoorActivityDestination queryTbOutdoorActivityDestinationByActivityId(Integer activityId) {
        TbOutdoorActivityDestinationExample example = new TbOutdoorActivityDestinationExample();
        TbOutdoorActivityDestinationExample.Criteria criteria =example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andIsDeleteEqualTo((byte) 0);
        List<TbOutdoorActivityDestination> destinations = tbOutdoorActivityDestinationMapper.selectByExample(example);
        if (destinations != null && destinations.size() == 1) {
            return destinations.get(0);
        }
        return null;
    }

    @Override
    public TbOutdoorDestination queryTbOutdoorDestinationById(Integer id) {
        return tbOutdoorDestinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean isExistWaitPayOrder(Integer uid, List<Integer> productIds) {
        TbOutdoorOrderExample example = new TbOutdoorOrderExample();
        TbOutdoorOrderExample.Criteria criteria = example.createCriteria();
        criteria.andOrderDisplayStatusEqualTo(OutdoorConstants.APP_TRAVEL_ORDER_WAIT_PAY);
        criteria.andUidEqualTo(uid);
        List<TbOutdoorOrder> list = tbOutdoorOrderMapper.selectByExample(example);
        if (null != list && list.size() >= OutdoorConstants.TRAVEL_MAX_WAIT_PAY_ORDER_NUM) {
            return true;
        }else if (null != list && list.size() ==1 ) {
            TbOutdoorOrder order = list.get(0);
            Gson gson = new Gson();
            List<Integer> ids = gson.fromJson(order.getProducts(), new TypeToken<List<Integer>>() {}.getType());
            if (null!=ids && null!=productIds) {
                ids.retainAll(productIds);
            }
            if (null!=ids && ids.size()>0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Integer> selectExpireActivity() {
        return vTravelUserDefinedMapper.selectExpireActivity();
    }

    @Override
    public List<Integer> indexNotShow() {
        return vTravelUserDefinedMapper.getIndexNotShwoActivity();
    }

    @Override
    public List<TbOutdoorActivity> selectNotExpireThemeActivityList() {
        TbOutdoorActivityExample example = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(OutdoorConstants.THEME_ACTIVITY_YES);
        criteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        return tbOutdoorActivityMapper.selectByExample(example);
    }

    @Override
    public List<ProductDetail> searchUsableProductDetail(String key, Integer pageNum, Integer pageSize) {
        return vTravelUserDefinedMapper.searchUsableProductDetail(key, (pageNum-1)*pageSize, pageNum*pageSize);
    }

    @Override
    public void cacheProductListByActivityId() {

    }


}
